package com.wyg.common.core.utils.file;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;

import com.wyg.common.core.exception.GlobalException;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.poi.util.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

/**
 * 图片处理工具类
 *
 * @author wyg
 */
public class ImageUtils
{
    private static final Logger log = LoggerFactory.getLogger(ImageUtils.class);

    public static byte[] getImage(String imagePath)
    {
        InputStream is = getFile(imagePath);
        try
        {
            return IOUtils.toByteArray(is);
        }
        catch (Exception e)
        {
            log.error("图片加载异常 {}", e);
            return null;
        }
        finally
        {
            IOUtils.closeQuietly(is);
        }
    }

    public static InputStream getFile(String imagePath)
    {
        try
        {
            byte[] result = readFile(imagePath);
            result = Arrays.copyOf(result, result.length);
            return new ByteArrayInputStream(result);
        }
        catch (Exception e)
        {
            log.error("获取图片异常 {}", e);
        }
        return null;
    }

    /**
     * 读取文件为字节数据
     * 
     * @param url 地址
     * @return 字节数据
     */
    public static byte[] readFile(String url)
    {
        InputStream in = null;
        try
        {
            // 网络地址
            URL urlObj = new URL(url);
            URLConnection urlConnection = urlObj.openConnection();
            urlConnection.setConnectTimeout(30 * 1000);
            urlConnection.setReadTimeout(60 * 1000);
            urlConnection.setDoInput(true);
            in = urlConnection.getInputStream();
            return IOUtils.toByteArray(in);
        }
        catch (Exception e)
        {
            log.error("访问文件异常 {}", e);
            return null;
        }
        finally
        {
            IOUtils.closeQuietly(in);
        }
    }

    public static InputStream resizeImage(InputStream inputStream, Integer maxSize) {

        try {

            //图片过大则进行压缩
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            //等比例压缩
            //Thumbnails.of(inputStream).scale(0.8).outputQuality(1).outputFormat("jpg").toOutputStream(outputStream);

            Thumbnails.of(inputStream).size(maxSize, maxSize).toOutputStream(outputStream);

            ByteArrayInputStream swapStream = new ByteArrayInputStream(outputStream.toByteArray());

            return swapStream;

        } catch (Exception e) {
            log.error("压缩图片失败", e);
            throw new GlobalException(e.getMessage());
        }

    }

    public static InputStream resizeImage(MultipartFile file, Integer maxSize) {
        InputStream inputStream = null;
        try {
            inputStream=file.getInputStream();
            return resizeImage(inputStream,maxSize);
        }
        catch (Exception ex){
            throw new GlobalException(ex.getMessage());
        }
        finally {
            if(inputStream!=null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
